import 'dart:math';
import 'package:safemap/safemap.dart';

import 'vehicle_data_base.dart';

// 实时信息类
class VehicleRealtimeInfo extends VehicleDataBase {
  // 电压  单位 0.01V
  double voltage = 0.0;

  // 电流  单位 0.01A
  double current = 0.0;

  // 速度  单位 0.01km/s
  double speed = 0.0;

  // 电机输出扭矩  单位 0.01N/m
  double torque = 0;

  // 输出矢量电压百分比  单位 0.01%  范围 0~10000
  double outputVectorVoltagePercentage = 0.0;

  // 电池输出功率  单位 1W
  double batteryOutputPower = 0;

  // 电机输出功率  单位 1W
  double motorOutputPower = 0;

  // 里程  单位 0.01km
  double mileage = 0.0;

  // 电池电量百分比  单位 0.01%
  double batteryPercentage = 0.0;

  // 剩余可行驶的电量百分比（剩余可行驶电量占总可行驶电量的比值） 单位 0.01%
  double estimatedRemainingMileage = 0.0;

  // 翘板速度 单位 0.01km/h
  double limitSpd = 0.0;

  // 报警速度 单位 0.01km/h
  double alarmSpd = 0.0;

  // mos管温度  单位 °C  偏置80，即数值0代表80°C
  int mosTemperature = 0;

  // 电机温度  单位 °C  偏置80，即数值0代表80°C
  int motorTemperature = 0;

  // 电池温度  单位 °C  偏置80，即数值0代表80°C
  int batteryTemperature = 0;

  // 控制器温度  单位 °C  偏置80，即数值0代表80°C
  int boardTemperature = 0;

  // 云盒信号强度，0-4
  int signalStrength = 0;

  // 电机相电流 单位 0.02A
  double iq = 0.0;

  VehicleRealtimeInfo.fromMap(Map<String, dynamic>? data)
      : super.fromMap(data) {
    SafeMap safeMap = SafeMap(data);

    iq = ((safeMap['iq'].toInt ?? 0) / 50).abs();
    voltage = (safeMap['bus_voltage'].toInt ?? 0) / 100;
    current = ((safeMap['bus_current'].toInt ?? 0) / 100).abs();
    speed = ((safeMap['speed'].toInt ?? 0) / 100).abs();
    torque = ((safeMap['torque'].toInt ?? 0) / 100).abs();
    outputVectorVoltagePercentage = (safeMap['output_rate'].toInt ?? 0.0) / 100;
    batteryOutputPower =
        ((safeMap['battery_output_power'].toInt ?? 0) / 1000).abs();
    motorOutputPower =
        ((safeMap['motor_output_power'].toInt ?? 0) / 1000).abs();
    mileage =
        (safeMap['mileage'].toInt ?? safeMap['single_mileage'].toInt ?? 0) /
            100;
    batteryPercentage =
        (safeMap['battery_percentage'].toInt ?? 0) / 10000; // 满电续航里程
    // 剩余可行驶的电量百分比
    final double remainingMileagePercentage =
        (safeMap['battery_percentage_for_ride'].toInt ?? 0) / 10000;
    final double fullChargeMileage =
        (safeMap['estimated_total_mileage'].toInt ?? 0) / 100;
    estimatedRemainingMileage = fullChargeMileage * remainingMileagePercentage;
    limitSpd = (safeMap['rt_over_speed_limit_threshold'].toInt ?? 0) / 100;
    alarmSpd = (safeMap['rt_over_speed_warning_threshold'].toInt ?? 0) / 100;
    mosTemperature = safeMap['mos_temperature'].toInt ?? 0;
    motorTemperature = safeMap['motor_temperature'].toInt ?? 0;
    batteryTemperature = safeMap['battery_temperature'].toInt ?? 0;
    boardTemperature = safeMap['board_temperature'].toInt ?? 0;
    signalStrength = min(safeMap['lte_signal'].toInt ?? 0, 4);
  }

  toMap() {
    return {
      'voltage': voltage,
      'current': current,
      'speed': speed,
      'torque': torque,
      'outputVectorVoltagePercentage': outputVectorVoltagePercentage,
      'batteryOutputPower': batteryOutputPower,
      'motorOutputPower': motorOutputPower,
      'mileage': mileage,
      'batteryPercentage': batteryPercentage,
      'estimatedRemainingMileage': estimatedRemainingMileage,
      'limitSpd': limitSpd,
      'alarmSpd': alarmSpd,
      'mosTemperature': mosTemperature,
      'motorTemperature': motorTemperature,
      'batteryTemperature': batteryTemperature,
      'boardTemperature': boardTemperature,
      'signalStrength': signalStrength,
      'iq': iq,
    };
  }
}
